﻿using System;
using System.Linq;
using TESTCommons.LinqUtil;
using TestDBData.EFContext;
using TestModel.SysManage;

namespace TestService.SysManage
{
    public partial class Sys_RoleNameService : ISys_RoleNameService
    {
        #region 参数
        private readonly IRepository<sys_RoleName> _RoleName;
        #endregion

        #region 构造函数
        public Sys_RoleNameService(IRepository<sys_RoleName> RoleName)
        {
            _RoleName = RoleName;
        }
        #endregion

        public void AddRoleName(sys_RoleName model)
        {
            _RoleName.Insert(model);
        }

        public void UpRoleName(sys_RoleName model)
        {
            _RoleName.Update(model);
        }

        public PagedList<sys_RoleName> QueryRoleName(string roleName, string userName, DateTime? fromUtc = null, DateTime? toUtc = null, int pageIndex = 0, int pageSize = int.MaxValue)
        {
            var expression = LinqExtensions.True<sys_RoleName>();
            if (!string.IsNullOrEmpty(roleName))
            {
                expression = expression.And(t => t.roleName == roleName);
            }
            if (!string.IsNullOrEmpty(userName))
            {
                expression = expression.And(t => t.userName == userName);
            }
            if (fromUtc.HasValue)
                expression = expression.And(t => t.createTime >= fromUtc);
            if (toUtc.HasValue)
                expression = expression.And(t => t.createTime <= fromUtc);
            var roles = _RoleName.Table.Where(expression.Compile()).ToList();
            return new PagedList<sys_RoleName>(roles, pageIndex, pageSize);
        }
    }
}
